using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 摄像机控制脚本，只能对正交摄像机使用（也就是2D摄像机）。
/// </summary>
[RequireComponent(typeof(Camera))]
public class CameraController : MonoBehaviour
{
    [Header("提示：此脚本只能对正交摄像机使用（2D相机）")]
    [Header("位置(跟随目标移动)")]
    public Transform followTarget;
    public float followSpeed = 5;
    private readonly float m_Depth = -10;
    [Header("大小（平滑式的改变大小）")]
    public float size = 6f;
    public float sizeChangeSpeed = 12;
    [Header("边界（最大显示界限和最小显示界限）")]
    public bool enableBoundsLimit = true;
    public Vector2 boundsMax = Vector2.zero;
    public Vector2 boundsMin = Vector2.zero;



    private Camera m_Camera;
    private void Run()
    {
        if (followTarget != null)
        {
            //改变摄像机位置
            Vector2 TargetCurrentPosition = followTarget.position;
            Vector2 CameraCurrentPosition = transform.position;
            float sqrDistance = (TargetCurrentPosition - CameraCurrentPosition).sqrMagnitude;
            sqrDistance = Mathf.Max(sqrDistance, 0.05f);
            Vector2 newPosition = Vector2.MoveTowards(transform.position, followTarget.position, followSpeed * Time.deltaTime * sqrDistance);
            if (enableBoundsLimit)
            {
                newPosition.x = Mathf.Clamp(newPosition.x, boundsMin.x + m_Camera.orthographicSize * m_Camera.aspect, boundsMax.x - m_Camera.orthographicSize * m_Camera.aspect);
                newPosition.y = Mathf.Clamp(newPosition.y, boundsMin.y + m_Camera.orthographicSize, boundsMax.y - m_Camera.orthographicSize);
            }
            transform.position = new Vector3(newPosition.x, newPosition.y, followTarget.position.z + m_Depth);
        }

        //改变摄像机大小
        if (m_Camera.orthographicSize != size)
        {
            m_Camera.orthographicSize = Mathf.MoveTowards(m_Camera.orthographicSize, size, Mathf.Max(Mathf.Abs(size - m_Camera.orthographicSize) * Time.deltaTime * sizeChangeSpeed, 0.07f));
        }
    }

    #region Unity方法
    private void Awake()
    {
        BuffDataManager.GetBuffData<Buff1>();
        m_Camera = GetComponent<Camera>();
        m_Camera.orthographic = true;
        PlayerController playerController = followTarget.GetComponent<PlayerController>();
        if (playerController != null)
        {
            playerController.OnScaleChange += (value) => { size += value; };
        }
    }
    private void FixedUpdate()
    {
        Run();
    }
    private void OnDrawGizmos()
    {
        if (enableBoundsLimit)
        {
            if (boundsMin.x > boundsMax.x || boundsMin.y > boundsMax.y)
            {
                Debug.LogError("最小界限不能比最大限界还要大！请检查设置。");
            }
            Gizmos.color = Color.green;
            Vector3 min = new Vector3(boundsMin.x, boundsMin.y, transform.position.z);
            Vector3 max = new Vector3(boundsMax.x, boundsMax.y, transform.position.z);
            Gizmos.DrawLine(min, new Vector3(max.x, min.y, transform.position.z));
            Gizmos.DrawLine(new Vector3(max.x, min.y, transform.position.z), max);
            Gizmos.DrawLine(max, new Vector3(min.x, max.y, transform.position.z));
            Gizmos.DrawLine(new Vector3(min.x, max.y, transform.position.z), min);
        }
    }

    #endregion
}
